"""__author__=Hanlin"""
import requests
from bs4 import BeautifulSoup

def get_page():
    url = 'http://sports.sina.com.cn/nba/?qq-pf-to=pcqq.group'
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
    }

    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        # 因为response.content返回的是字节流，decode之后变成字符串
        return response.content.decode('utf-8')
    return None


def parse_page(html):
    # html = '<div><span>坚持努力</span></div>'
    soup = BeautifulSoup(html, 'lxml')
    # prettify整齐的格式输出，会做一些补全
    # print(soup.prettify())
    # print(soup.title.string)
    # print(soup.head)
    # print(soup.p)
    # print(soup.p.name) # 打印的是标签的类型名
    # print(soup.img.attrs) # 出现的第一个img标签的所有的属性
    # print(soup.img.attrs['src'])
    # print(soup.p.contents) # 以列表的形式返回第一个p标签里面的所有的节点
    # print(list(soup.a.parents))

    # 用css定位的方式获取节点,空格可以表示子孙节点
    # a_list = soup.select('.news-list-b .list a')
    # for item in a_list:
    #     print(item.string)

    # 当class里面有多个样式名，需要把空格去掉
    a_list = soup.select('div.-live-layout-container.row-fuild .news-list-b .list a')
    for item in a_list:
        print(item.string)
def main():
    html = get_page()
    # print(html)
    parse_page(html)


if __name__ == '__main__':
    main()